home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / TPIFile / TPIFileTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.6 KB  |  306 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TPIFileTest.c
  3.  
  4.     Contains:    A trivial test program for the TPIFile module.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. /////////////////////////////////////////////////////////////////////
  22. // Pick up all the OT client info, specifically the OTRegisterPort.
  23.  
  24. #include <OpenTransport.h>
  25. #include <Events.h>
  26.  
  27. /////////////////////////////////////////////////////////////////////
  28. // OK, so it's yet another console based sample from Quinn!
  29.  
  30. #include <stdio.h>
  31. #include <limits.h>
  32.  
  33. /////////////////////////////////////////////////////////////////////
  34. // Pick up the name of the TPIFile port.
  35.  
  36. #include "TPIFile.h"
  37.  
  38. /////////////////////////////////////////////////////////////////////
  39.  
  40. // Define a global buffer to hold the data we're reading out of the file.
  41. // 
  42. // Note the number of bytes that we read out of the endpoint in a single
  43. // hit is not the same chunk size as used by the TPIFile module to read
  44. // the file (which is 2KB).  This is a deliberate test of the way OT
  45. // endpoint libraries do deblocking.
  46.  
  47. enum {
  48.     kBufferSize = 1000
  49. };
  50.  
  51. static char gBuffer[kBufferSize];
  52.  
  53. /////////////////////////////////////////////////////////////////////
  54.  
  55. static OSStatus TestTPIFile(ConstFSSpecPtr fss, UInt32 byteCount, Boolean disconnect)
  56.     // Test the TPIFile module by opening an endpoint to the
  57.     // file specified by fss and printing the contents to
  58.     // the console window.
  59. {
  60.     OSStatus err;
  61.     EndpointRef ep;
  62.     TCall sndCall;
  63.     FileSpecAddress connectAddr;
  64.     OTFlags junkFlags;
  65.     long i;
  66.     
  67.     // Open the endpoint to our module.
  68.     
  69.     ep = OTOpenEndpoint(OTCreateConfiguration(kTPIFilePortName), 0, nil, &err);
  70.     
  71.     // Switch to sync/blocking mode to simplify our code and then
  72.     // do a null bind in preparation for the connect.
  73.     if (err == noErr) {
  74.         (void) OTSetBlocking(ep);
  75.         (void) OTSetSynchronous(ep);
  76.         err = OTBind(ep, nil, nil);
  77.     }
  78.     
  79.     // Connect to a file specified by an AF_FILESPEC address format.
  80.     if (err == noErr) {
  81.         connectAddr.fAddressType = AF_FILESPEC;
  82.         connectAddr.fss = *fss;
  83.         sndCall.addr.buf = (UInt8 *) &connectAddr;
  84.         sndCall.addr.len = sizeof(connectAddr);
  85.         sndCall.opt.buf = nil;
  86.         sndCall.opt.len = 0;
  87.         sndCall.udata.buf = nil;
  88.         sndCall.udata.len = 0;
  89.         sndCall.sequence = 0;
  90.         err = OTConnect(ep, &sndCall, nil);
  91.     }
  92.     
  93.     // A standard OTRcv and print loop.
  94.     if (err == noErr) {
  95.         do {
  96.             err = OTRcv(ep, gBuffer, kBufferSize, &junkFlags);
  97.             if (err > noErr) {
  98.                 for (i = 0; i < err; i++) {
  99.                     if (byteCount == 0) {
  100.                         err = noErr;
  101.                         goto premature_close;
  102.                     }
  103.                     byteCount -= 1;
  104.                     if (gBuffer[i] == 13) {
  105.                         putchar(10);
  106.                     } else {
  107.                         putchar(gBuffer[i]);
  108.                     }
  109.                 }
  110.                 fflush(stdout);
  111.                 err = noErr;
  112.             }
  113.         } while (err == noErr);
  114.     }
  115.  
  116. premature_close:
  117.     if (err == noErr && disconnect) {
  118.         err = OTSndDisconnect(ep, nil);
  119.     }
  120.  
  121.     // When we can't read any more data, consult and print the reason.    
  122.     if (err == kOTLookErr) {
  123.         printf("Got kOTLookErr.\n");
  124.         err = OTLook(ep);
  125.         switch (err) {
  126.             case T_DISCONNECT:
  127.                 printf("T_DISCONNECT\n");
  128.                 break;
  129.             default:
  130.                 printf("Unknown look %d\n", err);
  131.         }
  132.         err = kOTLookErr;
  133.     }
  134.     
  135.     if (ep != kOTInvalidEndpointRef) {
  136.         (void) OTCloseProvider(ep);
  137.     }
  138.     return (err);
  139. }
  140.  
  141. /////////////////////////////////////////////////////////////////////
  142.  
  143. static OSStatus TestBogusConnect(void)
  144.     // Test how well the TPI module handles being asked to connect
  145.     // to a bogus FSSpec.
  146. {
  147.     OSStatus err;
  148.     FSSpec bogusFileSpec;
  149.     
  150.     (void) FSMakeFSSpec(0, 0, "\pBogus Victim", &bogusFileSpec);
  151.     
  152.     err = TestTPIFile(&bogusFileSpec, 1024, false);
  153.     
  154.     return (err);
  155. }
  156.  
  157. /////////////////////////////////////////////////////////////////////
  158.  
  159. static OSStatus TestConnectAsync(ConstFSSpecPtr fss, Boolean disconnect)
  160.     // Test an asynchronous connect operation followed immediately
  161.     // by a disconnect.
  162. {
  163.     OSStatus err;
  164.     EndpointRef ep;
  165.     TCall sndCall;
  166.     FileSpecAddress connectAddr;
  167.         
  168.     ep = OTOpenEndpoint(OTCreateConfiguration(kTPIFilePortName), 0, nil, &err);
  169.     
  170.     // Switch to sync/blocking mode to simplify our code and then
  171.     // do a null bind in preparation for the connect.
  172.     if (err == noErr) {
  173.         (void) OTSetBlocking(ep);
  174.         (void) OTSetSynchronous(ep);
  175.         err = OTBind(ep, nil, nil);
  176.     }
  177.     
  178.     // Connect to a file specified by an AF_FILESPEC address format.
  179.     if (err == noErr) {
  180.         (void) OTSetAsynchronous(ep);
  181.         connectAddr.fAddressType = AF_FILESPEC;
  182.         connectAddr.fss = *fss;
  183.         sndCall.addr.buf = (UInt8 *) &connectAddr;
  184.         sndCall.addr.len = sizeof(connectAddr);
  185.         sndCall.opt.buf = nil;
  186.         sndCall.opt.len = 0;
  187.         sndCall.udata.buf = nil;
  188.         sndCall.udata.len = 0;
  189.         sndCall.sequence = 0;
  190.         err = OTConnect(ep, &sndCall, nil);
  191.         if (err == kOTNoDataErr) {
  192.             err = noErr;
  193.             printf("Connecting\n");
  194.         }
  195.     }
  196.     if ( (err == noErr) && disconnect) {
  197.         err = OTSndDisconnect(ep, nil);
  198.         if (err == noErr) {
  199.             printf("Waiting for disconnect");
  200.             while ( OTGetEndpointState(ep) != T_IDLE ) {
  201.                 if ( TickCount() % 10 == 0 ) {
  202.                     printf(".");
  203.                 }
  204.             }
  205.             printf("\n");
  206.         }
  207.     }
  208.  
  209.     if (ep != kOTInvalidEndpointRef) {
  210.         (void) OTCloseProvider(ep);
  211.     }
  212.     
  213.     return (err);
  214. }
  215.  
  216. /////////////////////////////////////////////////////////////////////
  217.  
  218. static OSStatus TestConnectAsyncWithExtraEndpoint(ConstFSSpecPtr fss, Boolean disconnect)
  219. {
  220.     OSStatus err;
  221.     EndpointRef ep;
  222.     char junkStr[256];
  223.     
  224.     ep = OTOpenEndpoint(OTCreateConfiguration(kTPIFilePortName), 0, nil, &err);
  225.     if (err == noErr) {
  226.         err = TestConnectAsync(fss, disconnect);
  227.     }
  228.     if (err == noErr) {
  229.         printf("Press return to continue.\n");
  230.         gets(junkStr);
  231.     }
  232.     
  233.     return (err);
  234. }
  235.  
  236.  
  237. /////////////////////////////////////////////////////////////////////
  238.  
  239. void main(void)
  240. {
  241.     OSStatus err;
  242.     FSSpec fileToTest;
  243.     char commandStr[256];
  244.     
  245.     printf("Hello Cruel World!\n");
  246.     fflush(stdout);
  247.     
  248.     err = InitOpenTransport();
  249.     
  250.     if (err == noErr) {
  251.     
  252.         // Hardwire this code to read a file called "Victim" in the
  253.         // same folder as the application.
  254.         
  255.         err = FSMakeFSSpec(0, 0, "\pVictim", &fileToTest);
  256.  
  257.         if (err == noErr) {
  258.             printf("a) Read and print all.\n");
  259.             printf("b) Read and print with premature close.\n");
  260.             printf("c) Read and print with premature disconnect.\n");
  261.             printf("d) Connect to a bogus file.\n");
  262.             printf("e) Connect async then fast close.\n");
  263.             printf("f) Connect async then fast disconnect.\n");
  264.             printf("g) Connect async then fast close with extra endpoint.\n");
  265.             printf("Enter the test you want to perform:\n");
  266.             gets(commandStr);
  267.             
  268.             switch (commandStr[0]) {
  269.                 case 'a':
  270.                     err = TestTPIFile(&fileToTest, ULONG_MAX, false);
  271.                     break;
  272.                 case 'b':
  273.                     err = TestTPIFile(&fileToTest, 1024, false);
  274.                     break;
  275.                 case 'c':
  276.                     err = TestTPIFile(&fileToTest, 1024, true);
  277.                     break;
  278.                 case 'd':
  279.                     err = TestBogusConnect();
  280.                     break;
  281.                 case 'e':
  282.                     err = TestConnectAsync(&fileToTest, false);
  283.                     break;
  284.                 case 'f':
  285.                     err = TestConnectAsync(&fileToTest, true);
  286.                     break;
  287.                 case 'g':
  288.                     err = TestConnectAsyncWithExtraEndpoint(&fileToTest, false);
  289.                     break;
  290.                 default:
  291.                     printf("Huh?\n");
  292.                     err = -1;
  293.                     break;
  294.             }
  295.         }
  296.         
  297.         CloseOpenTransport();
  298.     }
  299.     
  300.     if (err == noErr) {
  301.         printf("Success.\n");
  302.     } else {
  303.         printf("Failed with error %d.\n", err);
  304.     }
  305.     printf("Done.  Press command-Q to Quit.\n");
  306. }